﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using BF.Model;
using BF.IBLL.BaseSystem;
using Base.Core.UnityFactory;

namespace BF.Web.App_Code
{
    public abstract class BaseHandler : IHttpHandler, IRequiresSessionState
    {

        /// <summary>
        /// 当前的ACTION test
        /// </summary>
        private string _action = string.Empty;  
        protected string Action
        {
            get { return _action; }
            set { _action = value; }
        }

        protected HttpContext _context = null;
        protected HttpRequest _request = null;
        protected HttpResponse _response = null;


        /// <summary>
        /// 获取排序后的集合 Ordery By sCode
        /// </summary>
        /// <param name="tableCode">表编码（大类编码）根的编码为:GEN</param>
        /// <returns></returns>
        protected List<dic_Dict> GetDictList(string tableCode)
        {
            try
            {
                List<dic_Dict> modelList = UICommon.DictList.Where(p => p.sTableCode == tableCode && p.iUse != 0).OrderBy(p => p.sCode).ToList();//查询条件：表编码 sTableCode
                return modelList.OrderBy(P => P.iSort).ToList();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + " 字段：" + tableCode + " 的值为空!");
            }
        }

        //获取action属性的值
        protected virtual string GetActionValue()
        {
            return GetParam("action");
        }

        /// <summary>
        /// 用户基础信息
        /// </summary>
        public BF.Model.sys_UserInfo GetSessionCurrentUserInfo
        {
            get
            {
                if (HttpContext.Current.Session["CurrentUserInfo_HIP"] == null)
                {
                    try
                    {
                        #region 扩展写业务系统中的userinfo表
                        Isys_UserInfoBLL iBLL = IOCFactory.GetIOCResolve<Isys_UserInfoBLL>();
                        BF.Model.sys_UserInfo userInfo = iBLL.GetModelByloginname(HttpContext.Current.User.Identity.Name);
                        #endregion
                        HttpContext.Current.Session["CurrentUserInfo_HIP"] = userInfo;
                    }
                    catch
                    {
                       HttpContext.Current.Response.Redirect("~/Login.aspx");
                    }
                    return HttpContext.Current.Session["CurrentUserInfo_HIP"] as BF.Model.sys_UserInfo;
                }
                else
                {
                    return HttpContext.Current.Session["CurrentUserInfo_HIP"] as BF.Model.sys_UserInfo;
                }
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            _context = context;
            _request = context.Request;
            _response = context.Response;
            _context.Response.ContentType = "text/plain";
            //if (_request.UrlReferrer == null)
            //{
            //    OutputResult(false, "未授权的访问.");
            //    return ;
            //}

            //if (_request.UrlReferrer.Host != _request.Url.Host)
            //{
            //    OutputResult(false, "未授权的访问.");
            //    return;
            //}
            _action = GetActionValue();//获取当前的ACTION


            ActionHandler(_action);

        }

        protected abstract void ActionHandler(string action);

        /// <summary>
        /// 获取REQUEST的参数值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected string GetParam(string key)
        {
            if (_context.Request[key] == null)
            {
                return string.Empty;
            }
            else
            {
                if (_context.Request[key].ToString().Trim() + "" == "")
                {
                    return string.Empty;
                }
                else
                {
                    return _context.Request[key].ToString().Trim();
                }
            }
        }

        /// <summary>
        /// 向客户端输出信息
        /// </summary>
        /// <param name="content"></param>
        protected void Output(string content)
        {
            _context.Response.Write(content);
        }
        // <summary>
        /// 向客户端输出信息(JSON)
        /// </summary>
        /// <param name="content"></param>
        protected void OutputResult(bool result, string content)
        {
            if (result)
                content = "{result:true,data:'" + content + "'}";
            else
                content = "{result:false,data:'" + content + "'}";
            _context.Response.Write(content);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}